package com.example.common.i18n.util;

import com.example.common.i18n.api.AjaxJson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.MessageSource;
import org.springframework.context.NoSuchMessageException;
import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;

import javax.servlet.http.HttpServletRequest;
import java.util.Locale;

@Slf4j
public class MessageUtils {

	private static final int SUCCESS = 0;
	private static final int FAIL = 1;

	private static MessageSource messageSource = SpringContextHolder.getBean(MessageSource.class);

	private static AcceptHeaderLocaleResolver localeResolver = SpringContextHolder.getBean(AcceptHeaderLocaleResolver.class);

	/**
	 * 根据消息键和参数 获取消息 委托给spring messageSource
	 *
	 * @param code 消息键
	 * @param args 参数
	 * @return
	 */
	public static String getMessage(String code, Object... args) {
		try {
			HttpServletRequest httpServletRequest = RequestUtil.getRequest();
			if (null != httpServletRequest && null != messageSource && null != localeResolver) {
				Locale locale = localeResolver.resolveLocale(httpServletRequest);
				log.info("当前语言环境：" + locale.getLanguage() + locale.getCountry());
				return messageSource.getMessage(code, args, locale);
			}
			return code;
		} catch (NoSuchMessageException e) {
			log.error("No message found under code" + code);
			return code;
		}
	}

	/**
	 * 根据消息键和参数 获取消息 委托给spring messageSource
	 *
	 * @param code 消息键
	 * @return
	 */
	public static String getMessage(String code) {
		if(messageSource==null){

		}
		try {
			HttpServletRequest httpServletRequest = RequestUtil.getRequest();
			if (null != httpServletRequest && null != messageSource && null != localeResolver) {
				Locale locale = localeResolver.resolveLocale(httpServletRequest);
				System.out.println("当前语言环境：" + locale.getLanguage());
				return messageSource.getMessage(code, null, locale);
			}
			return code;
		} catch (NoSuchMessageException e) {
			e.printStackTrace();
			return code;
		}
	}

	/**
	 * 根据消息键和参数 获取消息 委托给spring messageSource
	 *
	 * @param code 消息键
	 * @param args 参数
	 * @return
	 */
	public static String getMessage(String code, String defaultMessage, Object... args) {

		try {
			HttpServletRequest httpServletRequest = RequestUtil.getRequest();
			if (null != httpServletRequest && null != messageSource && null != localeResolver) {
				Locale locale = localeResolver.resolveLocale(httpServletRequest);
				System.out.println("当前语言环境：" + locale.getLanguage());
				return messageSource.getMessage(code, args, defaultMessage, locale);
			}
			return code;
		} catch (Exception e) {
			e.printStackTrace();
			return code;
		}
	}

	public static AjaxJson success() {
		return new AjaxJson("success", SUCCESS, null);
	}

	public static AjaxJson success(Integer code,String keyOrMsg,Object data) {
		String msg = getMessage(keyOrMsg);
		return new AjaxJson(msg,code,data);
	}

	public static AjaxJson error() {
		return new AjaxJson("failed", FAIL, null);
	}

	public static AjaxJson error(String keyOrMsg) {
		String msg = getMessage(keyOrMsg);
		return new AjaxJson(msg, FAIL, null);
	}

	public static AjaxJson error(Integer code, String key) {
		String msg = getMessage(key);
		return new AjaxJson(msg, code, null);
	}

}
